home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Voyeur 1.1.1 / Voyeur ƒ / v code ƒ / v file management.c < prev    next >
Text File  |  1994-02-27  |  3KB  |  116 lines

  1. /**********************************************************************\
  2.  
  3. File:        v file management.c
  4.  
  5. Purpose:    This module handles opening and closing files and forks.
  6.  
  7.  
  8. Voyeur -- a no-frills file viewer
  9. Copyright ©1993-4, Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "v file management.h"
  29. #include "program globals.h"
  30. #include "msg environment.h"
  31. #include "msg menus.h"
  32.  
  33. FSSpec            gTheFS[MAX_WINDOWS];
  34. int                gTheRefNum[MAX_WINDOWS];
  35. unsigned long    gTheOffset[MAX_WINDOWS][2];
  36. Boolean            gBufferChanged[MAX_WINDOWS];
  37. unsigned char    gTheBuffer[MAX_WINDOWS][256];
  38. unsigned long    gForkLength[MAX_WINDOWS][2];
  39. int                gWhichFork[MAX_WINDOWS];
  40. int                gWhichFile;
  41. int                gLastFile;
  42.  
  43. int OpenTheFile(FSSpec *myFSS, int *thisFile)
  44. {
  45.     OSErr            theError;
  46.     long            count;
  47.     CInfoPBRec        pb;
  48.     
  49.     pb.hFileInfo.ioCompletion=0L;
  50.     pb.hFileInfo.ioFDirIndex=0;    /* very important */
  51.     pb.hFileInfo.ioVRefNum=myFSS->vRefNum;
  52.     pb.hFileInfo.ioNamePtr=myFSS->name;
  53.     pb.hFileInfo.ioDirID=myFSS->parID;
  54.     theError=PBGetCatInfo(&pb, FALSE);
  55.     if (theError!=noErr)
  56.         return diskReadErr;
  57.     
  58.     gForkLength[gWhichFile][0]=pb.hFileInfo.ioFlLgLen;
  59.     gForkLength[gWhichFile][1]=pb.hFileInfo.ioFlRLgLen;
  60.     
  61.     gWhichFork[gWhichFile]=((gForkLength[gWhichFile][0]==0L) ? ((gForkLength[gWhichFile][1]==0L) ? 0 : 1) : 0);
  62.     
  63.     if (gWhichFork[gWhichFile]==0)
  64.         theError=OpenTheDataFork(myFSS, thisFile);
  65.     else
  66.         theError=OpenTheResourceFork(myFSS, thisFile);
  67.     
  68.     if (theError!=noErr)
  69.         return diskReadErr;
  70.     
  71.     gBufferChanged[gWhichFile]=FALSE;
  72.     gTheOffset[gWhichFile][0]=0L;
  73.     gTheOffset[gWhichFile][1]=0L;
  74.     
  75.     return GetBuffer(*thisFile, gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  76. }
  77.  
  78. void CloseTheFile(int fileRefNum)
  79. {
  80.     FSClose(fileRefNum);
  81. }
  82.  
  83. int GetBuffer(int fileRefNum, unsigned long off, unsigned char output[256])
  84. {
  85.     long            count;
  86.     OSErr            theError;
  87.     int                i;
  88.     
  89.     count=gForkLength[gWhichFile][gWhichFork[gWhichFile]]-off<256L ? gForkLength[gWhichFile][gWhichFork[gWhichFile]]-off : 256L;
  90.     SetFPos(fileRefNum, 1, off);
  91.     theError=FSRead(fileRefNum, &count, output);
  92.     if (count!=256L)
  93.     {
  94.         for (i=count; i<256; i++)
  95.             output[i]=0x00;
  96.     }
  97.  
  98.     return (theError==noErr) ? allsWell : diskReadErr;
  99. }
  100.  
  101. OSErr OpenTheDataFork(FSSpec *myFSS, int *thisFile)
  102. {
  103.     if (gHasFSSpecs)
  104.         return FSpOpenDF(myFSS, fsRdWrPerm, thisFile);
  105.     else
  106.         return HOpen(myFSS->vRefNum, myFSS->parID, myFSS->name, fsRdWrPerm, thisFile);
  107. }
  108.  
  109. OSErr OpenTheResourceFork(FSSpec *myFSS, int *thisFile)
  110. {
  111.     if (gHasFSSpecs)
  112.         return FSpOpenRF(myFSS, fsRdWrPerm, thisFile);
  113.     else
  114.         return HOpenRF(myFSS->vRefNum, myFSS->parID, myFSS->name, fsRdWrPerm, thisFile);
  115. }
  116.